//
//  TimeTableView.swift
//  ANOA
//
//  Created by 邱仙凯 on 2024/9/25.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit

class PointProcessView: UITableView {
    var points = [TodopProgress]()
    var timelineHeight:[CGFloat] = []
    var vc:UIViewController?
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        self.delegate = self
        
        self.dataSource = self
        self.register(AuditLineCell.self, forCellReuseIdentifier: AuditLineCell.identifier)

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}
extension PointProcessView:UITableViewDelegate,UITableViewDataSource {
     
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return points.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.timelineHeight[indexPath.row]
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: AuditLineCell.identifier, for: indexPath) as? AuditLineCell else {
            fatalError()
        }
        let one = points[indexPath.row]
        cell.settitle(one: one)
        var immms = ""
        if one.nodeType == "ROOT" {
            immms =  "af_sh_ren"
 
        } else if one.result == "agree" {
            immms =  "af_sh_1"
         }  else if one.taskstats == "处理中" {
            immms = "af_sh_2"
         } else {
            immms =  "af_weidao"
 
        }
        if indexPath.row == 0 {
            cell.topline.isHidden = true
            cell.bottomline.isHidden = false
        } else if indexPath.row == points.count - 1 {
            cell.topline.isHidden = false
            cell.bottomline.isHidden = true
        } else {
            cell.topline.isHidden = false
            cell.bottomline.isHidden = false
        }
        
        cell.timeLab.text = one.finishTime
        cell.jueseimg.image = UIImage(named: immms)

        var approvalNm = ""
        for item in one.userinfo {
            let namex = item.user ?? "[人员]"
            approvalNm += "\(namex)  "
        }
        cell.contentLab.text = approvalNm
        var comments = [commentModel]()
        var attachs = [attachmentsModel]()
        for item in one.userinfo {
            for com in item.comment {
                comments.append(com)
            }
            for atta in item.attachments {
                attachs.append(atta)
            }
        }
        cell.setcomments(comments: comments)
        cell.setattachs(attachs: attachs)

    
        let tap1 = UITapGestureRecognizer()
        tap1.numberOfTapsRequired = 1
        tap1.addTap { [one] _ in
            let ids = one.userinfo.compactMap{$0.userid}.joined(separator: ",")
            showUserInfoArray(idkey:ids)
        }
        cell.contentLab.addGestureRecognizer(tap1)
   
        cell.attachTouch = { tag in
            let atta = attachs[tag]
            var url: String
            if checkPlatform(type: .zhuYouAn) {
                url = configURLAudit(latter: atta.url.unwrap)
            } else {
                url = configURLOK(latter: atta.url.unwrap)
            }
            let cwebV = CommonWebController()
            cwebV.reportUrl = url
            cwebV.title = atta.name.unwrap
            self.vc?.navigationController?.pushViewController(cwebV, animated: true)
        }
        
        
       
        
        return cell
    }
   
}


class AuditLineCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        //        self.accessoryType = .disclosureIndicator
        if commentlabArr.count == 0 {
            for i in 0..<10 {
                let lab = creatCommentLab()
                self.contentView.addSubview(lab)
                commentlabArr.append(lab)
            }
        }
        if attachlabArr.count == 0 {
            for i in 0..<10 {
                let lab = creatAttachLab()
                self.contentView.addSubview(lab)
                attachlabArr.append(lab)
            }
        }
        
        
        self.contentView.addSubview(jueseimg)
        self.contentView.addSubview(timeLab)
 
        

        self.contentView.addSubview(titleLab)
        self.contentView.addSubview(topline)
        self.contentView.addSubview(bottomline)
        
        self.contentView.addSubview(contentLab)
        
        
     
        
        
        jueseimg.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.height.equalTo(25)
            make.left.equalToSuperview().offset(5)
        }
        topline.snp.makeConstraints { make in
            make.centerX.equalTo(jueseimg)
            make.width.equalTo(3)
            make.top.equalToSuperview()
            make.bottom.equalTo(jueseimg.snp.top)
        }
        bottomline.snp.makeConstraints { make in
            make.centerX.equalTo(jueseimg)
            make.width.equalTo(3)
            make.bottom.equalToSuperview()
            make.top.equalTo(jueseimg.snp.bottom)
        }
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(jueseimg.snp.right).offset(6)
            make.right.equalTo(timeLab.snp.left)
            make.top.equalTo(self.contentView).offset(10)
        }
        
        timeLab.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalTo(titleLab)
            make.width.greaterThanOrEqualTo(148)
        }
        contentLab.snp.makeConstraints { make in
            make.left.equalTo(titleLab)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(titleLab.snp.bottom).offset(5)
        }
      
//
//        attahLab.snp.makeConstraints { make in
//            make.left.equalTo(titleLab)
//            make.right.equalToSuperview().offset(-10)
//            make.top.equalTo(commentLab.snp.bottom).offset(8)
//        }
    }
    lazy var topline:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex:"#aaaaaa")
        return v
    }()
    lazy var bottomline:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex:"#aaaaaa")
        return v
    }()
    lazy var jueseimg:UIImageView = {
        let view = UIImageView()
        return view
    }()
 
    private lazy var titleLab:UILabel = {
        let lab = UILabel()
        lab.numberOfLines = 2
        lab.font = UIFont.systemFont(ofSize: 14)
        return lab
    }()
   
    lazy var timeLab:UILabel = {
        let lab = UILabel()
        lab.textAlignment = .right
        lab.font = UIFont.systemFont(ofSize: 14)
        return lab
    }()

    lazy var contentLab:UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 14)
         lab.numberOfLines = 2
        lab.isUserInteractionEnabled = true
        return lab
    }()
    var commentlabArr:[UILabel] = []
    var attachlabArr:[UILabel] = []

    func creatCommentLab() -> UILabel {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 14)
         lab.numberOfLines = 0
        lab.backgroundColor = UIColor(kshex: "f5f5f5")
        lab.layer.masksToBounds = true
        lab.layer.cornerRadius = 5
        lab.isUserInteractionEnabled = true
        return lab
    }
    func creatAttachLab() -> UILabel {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 14)
         lab.numberOfLines = 0
        lab.backgroundColor = UIColor(kshex: "AEEEEE")
        lab.layer.masksToBounds = true
        lab.layer.cornerRadius = 5
        lab.isUserInteractionEnabled = true
        return lab
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var commentCount = 0
    func settitle(one:TodopProgress) {
        var ssColor:UIColor!
        if one.nodeType == "ROOT" {
             ssColor = UIColor(kshex: "a3c954")
         } else if one.result == "agree" {
             ssColor = UIColor(kshex: "a3c954")
        }  else if one.taskstats == "处理中" {
             ssColor = UIColor(kshex: "e69c37")
         } else {
             ssColor = UIColor(kshex: "aaaaaa")
         }
        let statusStr = "(\(one.taskstats.unwrap))"
        let ttS = one.title.unwrap + statusStr
        self.titleLab.attributedText(completeString: ttS, colorText: [(statusStr,ssColor)])

 
        
    }
    func setcomments(comments:[commentModel]) {
        for i in 0..<commentlabArr.count {
            let  lab = commentlabArr[i]
            if comments.count > i {
                lab.isHidden = false
                lab.text = comments[i].user.unwrap + ":" + comments[i].comment.unwrap
                    
                    lab.snp.makeConstraints { make in
                        make.left.equalTo(self.titleLab)
                        if i == 0 {
                            make.top.equalTo(contentLab.snp.bottom).offset(3)
                        } else {
                            let beforeLab = commentlabArr[i-1]
                            make.top.equalTo(beforeLab.snp.bottom).offset(3)

                        }
                        make.right.equalTo(timeLab)
                    }
                
            } else {
                lab.isHidden = true
            }
         
        }
        self.commentCount = comments.count
    }
    var attachTouch:((Int)->())?
    func setattachs(attachs:[attachmentsModel]) {
        for i in 0..<attachlabArr.count {
            let  lab = attachlabArr[i]
            
            let tap2 = UITapGestureRecognizer()
            tap2.numberOfTapsRequired = 1
            tap2.addTap {_ in
                if let clo = self.attachTouch {
                    clo(i)
                }
  
            }
            lab.addGestureRecognizer(tap2)
            
            if attachs.count > i {
                lab.isHidden = false
                lab.text = attachs[i].name.unwrap
                lab.snp.makeConstraints { make in
                    make.left.equalTo(self.titleLab)
                    if i == 0 {
                    
                        if self.commentCount > 0 {
                            let clab = self.commentlabArr[self.commentCount - 1]
                            make.top.equalTo(clab.snp.bottom).offset(3)
                        } else {
                            make.top.equalTo(contentLab.snp.bottom).offset(3)

                        }
                        
                        
                    } else {
                        let beforeLab = attachlabArr[i-1]
                        make.top.equalTo(beforeLab.snp.bottom).offset(3)

                    }
                    make.right.equalTo(timeLab)
                    make.height.equalTo(23)
                }
            } else {
                lab.isHidden = true
            }
        
           
            
        }
   
      
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
